package com.tevintao.marketingsystem.task;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.tevintao.marketingsystem.log.LogManager;
import com.tevintao.marketingsystem.services.TaskLogicService;
import com.tevintao.marketingsystem.services.ViewOperation;
import com.tevintao.marketingsystem.xposted.HookWindowManagerGlobal;
import com.tevintao.marketingsystem.xposted.WxTaskManger;

import org.json.JSONObject;

import java.lang.reflect.Field;
import java.util.Map;
import java.util.Random;


public class BrowFriendTaskProcess extends SetTaskExtra {
    private static BrowFriendTaskProcess ourInstance = new BrowFriendTaskProcess();

    public static BrowFriendTaskProcess getInstance() {
        return ourInstance;
    }

    private BrowFriendTaskProcess() {
    }

    private int mSelectPosition = -1;
    private Random mRandom = new Random();

    @SuppressWarnings("ResourceType")
    @Override
    public boolean processView(String contextName, View v) {
        if (WxUiViewConfig.MAIN_LAUNCH.equals(contextName)) {
            boolean inDiscover = WxUiViewConfig.inTab(v, WxUiViewConfig.TAB_DISCOVER);
            if (inDiscover) {
                View friendView = WxUiViewConfig.getViewByIdAndContent((ViewGroup) v, WxUiViewConfig.DISCOVER_LIST_ITEM_ID, "朋友圈");
                if (null != friendView) {
                    ViewOperation.clickView(friendView);
                }
            }
            mSelectPosition = -1;
        } else if (WxUiViewConfig.CIRCLE_OF_FRIENDS.equals(contextName)) {
//            WxUiViewConfig.printViewGroup((ViewGroup) v);
            View newView = v.findViewById(2131627706);
            if (null != newView && newView.getVisibility() == View.VISIBLE && newView instanceof TextView) {
                TextView newTextView = (TextView) newView;
                String context = newTextView.getText().toString();
                if (!TextUtils.isEmpty(context)) {
                    ViewOperation.clickView(newView);
                    return true;
                }
            }
            if (mRandom.nextInt(100) > 70) {
                return false;
            }
            View listView = v.findViewById(WxUiViewConfig.CIRCLE_OF_FRIENDS_LIST_VIEW);
            if (null != listView && listView instanceof ListView) {
                ListView list = (ListView) listView;
                ViewOperation.moveListPosition(list, mSelectPosition);
                mSelectPosition++;
                if (mSelectPosition > 10) {
                    mSelectPosition = mRandom.nextInt(30);
                }
            }
        } else if (WxUiViewConfig.CIRCLE_OF_FRIENDS_NEW_MESSAGE.equals(contextName)) {
            TaskLogicService.sendKeyEvent(v.getContext(), TaskLogicService.INPUT_TYPE_BACK);
        }
        return false;
    }


}
